package simpleUsers;

import java.util.ArrayList;
import java.util.List;

public class UsersHandler {
	private ArrayList<Member> users;

	public UsersHandler (){
		this.users = new ArrayList<Member>();
		this.setHardCodedModerators();
	}

	private void setHardCodedModerators() {
		this.users.add(new Member("Masha", "Maria", "Kertsburd", "gxyze", "mariaker21@gmail.com", 'f'));
		this.users.get(0).setModeratorRole();
		this.users.add(new Member ("Olgasib", "Olga", "Sibyriov", "1234", "olgasbrv@gmail.com", 'f'));
		this.users.get(1).setModeratorRole();
		this.users.add(new Member ("Alonsgl", "Alon", "Segel", "5678", "alon.sgl@gmail.com", 'm'));
		this.users.get(2).setModeratorRole();
		this.users.add(new Member ("Batelbbe", "Batel", "Ben-Ezra", "10101", "batel.bee@gmail.com", 'f'));
		this.users.get(3).setModeratorRole();
		this.users.add(new Member ("NaveAriel", "Nave", "Ariel", "98989", "nave.ariel8@gmail.com", 'm'));
		this.users.get(4).setModeratorRole();
	}

	/**
	 * This method adds new user to the list of registered users.
	 * @param newUser: the user to add
	 * @return true if the adding was successful and false otherwise
	 * !!!!!!!!!!!!!!!!!!!!!!!!!Important!!! Will be Useful For REGISTRATION!!!!!!!!!!!!!!!!!
	 */
	public synchronized boolean addUser (Member newUser){
		return this.users.add(newUser);
	}

	/**
	 * This method removes the user with given username and also removes him from
	 * 								other users' friends' lists
	 * @param username: the username of the user to be removed
	 * @return username of removed user if the removing was successful and false otherwise
	 * !!!!!!!!!!!!!!!!Important!!! assuming that the check if the other user has the
	 * 								authority to remove this user was done in STORAGE!!!!!!!
	 */
	public synchronized String removeUser (String username){
		int index = findUser(username);
		if (index != -1){
			for (int i = 0; i < this.users.size(); i++){
				this.users.get(i).removeFriend(username);
			}
			return this.users.remove(index).getUsername();
		}
		return "";
	}

	/**
	 * @param username: the name of the user to find.
	 * @return the index of theuser in the list if user with given username exists
	 *	       and -1 otherwise.
	 */
	public synchronized int findUser(String username) {
		for (int i = 0; i < this.users.size(); i++)
			if (this.users.get(i).getUsername().equals(username))
				return i;
		return -1;
	}

	/**
	 * This method adds a specified friend to specified user's list of friends
	 * @param username: the user who wants to add the friend
	 * @param friendName: the name of the friend to add
	 * @return true if the adding was successful
	 *         and false if the adding has failed.
	 */
	public synchronized boolean addUserFriend(String username, String friendName){
		int userIndex = findUser(username);
		return this.users.get(userIndex).addFriend(friendName);
	}

	/**
	 * This method removes a specified friend from specified user's list
	 * @param username: the user who wants to remove friend.
	 * @param friendName: the name of the friend to remove
	 * @return the name of the removed friend if both user and friend exist in the list
	 * 			and empty string if one of them does not exist in the list or the removing
	 * 			has failed.
	 */
	public synchronized String removeUserFriend(String username, String friendName){
		int userIndex = findUser(username);
		int friendIndex = findUser(friendName);
		if (userIndex != -1 && friendIndex != -1){
			return this.users.get(userIndex).removeFriend(friendName);
		}
		return "";
	}

	/**
	 * This method changes the status of the user who wishes to log in to logged in.
	 * @param username: the name of the user to log in
	 * @return true if the user exists in the list and he is logged in successfully
	 *         and false otherwise.
	 */
	public synchronized boolean userLoggedIn(String username, String password){
		int index = findUser(username);
		if (index != -1){
			if (this.users.get(index).getPassword().equals(password)){
				this.users.get(index).login();
				return true;
			}
		}
		return false;
	}

	/**
	 * This method changes the status of the user who wishes to log in to logged in.
	 * @param username: the name of the user to log in
	 * @return true if the user exists in the list and he is logged off successfully
	 *         and false otherwise.
	 */
	public synchronized boolean userLoggedOff(String username){
		int index = findUser(username);
		if (index != -1){
			this.users.get(index).logoff();
			return true;
		}
		return false;
	}

	/**
	 * This method returns the list of registered users' usernames.
	 * @return the list of registered users' usernames.
	 * !!!!!!!!!!!!!!!!!!!Important!!! Don't return the list such it is because we don't want
	 * 						anyone outside of this class to be able to see and maybe even
	 * 						change users' data.
	 */
	public synchronized List<String> getUsers(){
		List<String> namesOfRegisteredUsers = new ArrayList<String>();
		for (int i = 0; i < this.users.size(); i++){
			namesOfRegisteredUsers.add(this.users.get(i).getUsername());
		}
		return namesOfRegisteredUsers;
	}

	////////////////////////////////////////TO DELETE AFTER TESTS!!!!///////////////////
	public synchronized ArrayList<Member> getTotalUsers(){
		return this.users;
	}

	/**
	 * @return the number of registered users.
	 */
	public int numOfUsers(){
		return this.users.size();
	}


	/**
	 * @param username: the name of the user to check.
	 * @return true if thie user exists and he is a moderator
	 * 			and false if he does not exist or he is not a moderator.
	 */
	public synchronized boolean isModerator(String username){
		int index = findUser(username);
		if (index != -1){
			if (this.users.get(index).getRole().equalsIgnoreCase("moderator"))
					return true;
		}
		return false;
	}

	public synchronized boolean setModerator(String username, String userToSet){
		int index1 = findUser(username);
		int index2 = findUser(userToSet);
		if (index1 != -1 && index2 != -1){
			if (isModerator(username)){
				this.users.get(index2).setModeratorRole();
				return true;
			}
		}
		return false;
	}

	public synchronized boolean isUserLoggedin(String username){
		int index = findUser(username);
		if (index != -1)
			return this.users.get(index).getStatus();
		return false;
	}

        public synchronized ArrayList<String> getListOfFriends(String username){
        	ArrayList<String> ans = new ArrayList<String>();
                int index = findUser(username);
                if (index != -1){
                    ans = this.users.get(index).getFriends();
                }
                return ans;
        }

}
